<?php
	require_once(dirname(__FILE__)."/../../inc/product_auth.php");
?>
<?php
	include(dirname(__FILE__)."/../../../../../inc/class/proxy/BaseProxy.abstract.php");
?>
<?php
class WFProxy extends ABaseProxy {
	/*
	protected $cgiArgs; // all cgi parameters are stored in this associative array
	protected $errCode; // for error checking
	protected $errMessage; // for error checking (Note: only for debug, not for UI)

	protected $httpObj; // easy http communication object
	protected $serverInfo; // use this object to retrieve server information
	*/

	// Proxy Init
	public function proxy_init() 
	{
		if (!isset($this->cgiArgs['serverid']))
		{
			$this->errCode = WF_PROXY_ERR_INIT_INVALID_INPUT;
			$this->errMessage = WF_PROXY_ERR_INIT_INVALID_INPUT_MSG;
		}
	}

	// Proxy Exec
	public function proxy_exec() 
	{
		// localhost, directly launch report.php
		if ($this->cgiArgs['serverid'] == '1')
		{      

		}
		else
		{
			// Have we already login?
			$mySessionInfo = $this->LoadSessionInfo();
			if( $mySessionInfo != null )
			{
				/*
				 * there are stored session info, to see does it work.
				 */
				// 1. we need sid and cookie
				if( isset($mySessionInfo['cookie']) ) {
					// 2. set cookie
					$this->httpObj->setCookieStr($mySessionInfo['cookie']);
					// 3. send the request to report.php directly
					if( !$this->RetriveData() )
					{ 
						$this->errCode = WF_PROXY_ERR_EXEC_CONNECT;
						$this->errMessage = $this->httpObj->getErrMessage();
					}
					$mybody=$this->httpObj->getBody();	
					$posstart=strpos($mybody,"../help/Summary.htm");
					if( $this->httpObj->getCode() == 200 && $posstart!=FALSE) {
						// yes, we did.
						return;
					}
				}
			}

			// login
			if (!$this->Login())
			{
				return;
			} 
			
			// retrive data
			if (!$this->RetriveData())
			{ 
				$this->errCode = WF_PROXY_ERR_EXEC_CONNECT;
				$this->errMessage = $this->httpObj->getErrMessage();
			}
		}
	}
	
	private function VirusDetection(&$HTMLDom,&$XMLDom,$days)
	{
			$datatrs=$HTMLDom->getElementsByTagName("table")->item(3)->getElementsByTagName("tr")->item(2)->getElementsByTagName("tr");
			$XMLDom->encdoing= 'UTF-8';
			$elVirusDetection=	$XMLDom->CreateElementNS('http://www.trendmicro.com/tmsplx','VirusDetection');
			
			$elItemuncleanable=$XMLDom->createElement('item');
			$elItemuncleanable->setAttribute('title','Virus uncleanable');
			if($days==0)
				$elItemuncleanable->setAttribute('count',$datatrs->item(1)->getElementsByTagName("td")->item(1)->nodeValue);
			else
				$elItemuncleanable->setAttribute('count',$datatrs->item(1)->getElementsByTagName("td")->item(2)->nodeValue);
			$elVirusDetection->appendChild($elItemuncleanable);
			
			$elItemquarantined=$XMLDom->createElement('item');
			$elItemquarantined->setAttribute('title','Virus quarantined');
			if($days==0)
				$elItemquarantined->setAttribute('count',$datatrs->item(2)->getElementsByTagName("td")->item(1)->nodeValue);
			else
				$elItemquarantined->setAttribute('count',$datatrs->item(2)->getElementsByTagName("td")->item(2)->nodeValue);
			$elVirusDetection->appendChild($elItemquarantined);
			
			$elItemdeleted=$XMLDom->createElement('item');
			$elItemdeleted->setAttribute('title','Virus deleted');
			if($days==0)
				$elItemdeleted->setAttribute('count',$datatrs->item(3)->getElementsByTagName("td")->item(1)->nodeValue);
			else
				$elItemdeleted->setAttribute('count',$datatrs->item(3)->getElementsByTagName("td")->item(2)->nodeValue);
			$elVirusDetection->appendChild($elItemdeleted);
			
			$elItempassed=$XMLDom->createElement('item');
			$elItempassed->setAttribute('title','Virus passed');
			if($days==0)
				$elItempassed->setAttribute('count',$datatrs->item(4)->getElementsByTagName("td")->item(1)->nodeValue);
			else
				$elItempassed->setAttribute('count',$datatrs->item(4)->getElementsByTagName("td")->item(2)->nodeValue);
			$elVirusDetection->appendChild($elItempassed);
			
			$elItemcleaned=$XMLDom->createElement('item');
			$elItemcleaned->setAttribute('title','Virus cleaned');
			if($days==0)
				$elItemcleaned->setAttribute('count',$datatrs->item(5)->getElementsByTagName("td")->item(1)->nodeValue);
			else
				$elItemcleaned->setAttribute('count',$datatrs->item(5)->getElementsByTagName("td")->item(2)->nodeValue);
			$elVirusDetection->appendChild($elItemcleaned);
			
			$elItemrenamed=$XMLDom->createElement('item');
			$elItemrenamed->setAttribute('title','Virus renamed');
			if($days==0)
				$elItemrenamed->setAttribute('count',$datatrs->item(6)->getElementsByTagName("td")->item(1)->nodeValue);
			else
				$elItemrenamed->setAttribute('count',$datatrs->item(6)->getElementsByTagName("td")->item(2)->nodeValue);
			$elVirusDetection->appendChild($elItemrenamed);
			
			$XMLDom->appendChild($elVirusDetection);
	}

	// Proxy Result
	public function proxy_output() 
	{
		header('Content-type: application/xml;charset=utf-8');
		
		if ($this->cgiArgs['serverid'] == '1')		// local
		{

		} 
		else
		{ 
			$dom =new DOMDocument();
			$output =new DOMDocument();
			$dom->loadHTML($this->httpObj->getBody());
			$this->VirusDetection($dom,$output,(int)$this->cgiArgs['D']);
			echo $output->saveXML();
		}
	}
	
	// Proxy Check
	public function proxy_check()
	{  
		if ($this->cgiArgs['serverid'] == '1')		// local 
			return TRUE;
		else 
		{
			// Have we already login?
			$mySessionInfo = $this->LoadSessionInfo();
			if( $mySessionInfo != null )
			{
				/*
				 * there are stored session info, to see does it work.
				 */
				// 1. we need cookie
				if(  isset($mySessionInfo['cookie']) ) {
					// 2. set cookie
					$this->httpObj->setCookieStr($mySessionInfo['cookie']);
					// 3. send the request 
					$URL = "/SProtectLinux/summary.cgi";
					$this->httpObj->setPath($URL);
					$this->httpObj->setMethod_GET();
					$this->httpObj->setCGI("page","../html/summary.htm");
					if( $this->httpObj->Send() == TRUE ) {
					$mybody=$this->httpObj->getBody();	
					$posstart=strpos($mybody,"../help/Summary.htm");
						if( $this->httpObj->getCode() == 200  && $posstart!=FALSE) {
							// yes, we did.
							mydebug_log("no need to login");
							return TRUE;
						}
					}
				}
			}
			// oh, we need to relogin
			mydebug_log("login");
			return $this->Login();
		}
	}

	private function Login()
	{
		// make sure the path
		$this->httpObj->setPath('/SProtectLinux/scanoption_set.cgi?');
		$this->httpObj->setMethod_POST(true);
		$this->httpObj->setCGI( "tfPassword", "");
		$this->httpObj->setCGI( "id", "LogonPassword");
		$this->httpObj->setCGI( "T1", md5($this->serverInfo['password']));

		if( $this->httpObj->Send() == FALSE ) {
			if($this->httpObj->getErrCode()===28)
			{
				$this->errCode = WF_PROXY_ERR_EXEC_TIMEOUT;
			}
			else
			{
				$this->errCode = WF_PROXY_ERR_EXEC_CONNECT;
			}
			$this->errMessage = $this->httpObj->getErrMessage();
			return FALSE;
		}

    $mybody=$this->httpObj->getBody();	
		$posstart=strpos($mybody,"document.cookie=");
				
		// Login Success 
		if (($this->httpObj->getCode() == 200) && $posstart!== FALSE)
		{
			// store session info for next request
			$secretstr=substr($mybody,$posstart+17,130);
			$this->httpObj->setCookieStr($secretstr);
			$this->SaveLoginInfo($secretstr);
			return TRUE;
		}
		// Login Failed
		else										
		{ 
			$this->errCode = WF_PROXY_ERR_EXEC_LOGIN;
			$this->errMessage = WF_PROXY_ERR_EXEC_LOGIN_MSG."(".__FILE__ . ":" . __LINE__.")";
			return FALSE;
		}
/*		
		echo "\n\n";
		$tarray = $this->httpObj->getHeaders();
		print_r($tarray);
		echo "<h1>Body</h1><BR>\n";
		echo $this->httpObj->getBody();
*/ 
	}

	private function RetriveData()
	{
		$URL = "/SProtectLinux/summary.cgi";
		$this->httpObj->setPath($URL);
		$this->httpObj->setMethod_GET();
		$this->httpObj->setCGI("page","../html/summary.htm");
		if( $this->httpObj->Send() == FALSE ) {
			if($this->httpObj->getErrCode()===28)
			{
				$this->errCode = WF_PROXY_ERR_EXEC_TIMEOUT;
			}
			else
			{
				$this->errCode = WF_PROXY_ERR_EXEC_CONNECT;
			}
			$this->errMessage = $this->httpObj->getErrMessage();
			return FALSE;
		}
		return TRUE;
	}

	private function ServerKey()
	{
		return $this->serverInfo['protocol'].$this->serverInfo['username'].$this->serverInfo['password'].$this->serverInfo['host'].$this->serverInfo['port'];
	}

	private function LoadSessionInfo()
	{
		$key = $this->ServerKey();
		if( $this->session_info != null && isset($this->session_info[$key]) ) {
			return $this->session_info[$key];
		}
		return null;
	}

	private function SaveLoginInfo($cookie)
	{
		$key = $this->ServerKey();
		$sinfo = array();
		$sinfo['cookie'] = $cookie;
		$this->session_info[$key] = $sinfo;
		$this->save_sinfo();
	}
}
?>
